<script setup>
import { ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'


const loading = ref(false)    // 加载状态
const router = useRouter()    // 路由
const form = ref()            // 表单对象
const formModel = ref({       // 表单数据对象
  username: '',
  password: '',
})

// 非空校验
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 11, message: '账号长度不能小于3大于11', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur' }
  ],
}

// 登录操作
const goLogin = async () => {
  try {
    loading.value = true
    // 执行表单验证
    await form.value.validate();
    // 表单验证通过
    console.log('表单验证成功');
    // 在这里执行登录逻辑
    ElMessage.success('登录成功');
    loading.value = false
    router.push('/')
  } catch (error) {
    // 表单验证失败
    console.error('表单验证失败');
    ElMessage.error('账号或密码有误');
    loading.value = false
  }
}
</script>

<template>
  <div class="login-view">
    <div class="login-container" v-loading="loading">
      <div class="login-title">会员管理系统</div>
      <el-form :model="formModel" ref="form" :rules="rules" size="large" autocomplete="off">
        <el-form-item prop="username">
          <el-input v-model="formModel.username" placeholder="请输入账号" clearable :prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formModel.password" placeholder="请输入密码" clearable show-password :prefix-icon="Lock" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" size="large" color="#2a438a" @click="goLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-view {

  min-height: 100vh;
  background-image: url('../../assets/image/login-bg.jpg');
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;

  .login-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #ffffff;
    padding: 30px 100px;
    border-radius: 10px;

    .login-title {
      font-size: 30px;
      font-weight: bold;
      text-align: center;
      background-color: inherit !important;
      color: #2a438a;
      margin: 0 0 20px 0;
      width: 350px;
    }
  }
}
</style>
